import java.util.Arrays;
/**
 *双指针 -> 复写零
 *
 */
class Solution {
    public void duplicateZeros(int[] arr) {
        int dest = -1;
        int cur = 0;
        while (dest < arr.length-1) {
            if (arr[cur] != 0) {
                dest++;
                if (dest < arr.length-1) {
                    cur++;
                }
            }else {
                dest += 2;
                if (dest < arr.length-1) {
                    cur++;
                }
            }
        }
        while (cur >= 0) {
            if (dest >= arr.length) {
                arr[dest-1] = 0;
                dest -= 2;
                cur--;
                continue;
            }
            if (arr[cur] != 0) {
                arr[dest--] = arr[cur--];
            }else {
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }
        }

    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] arr = {1,0,2,3,0,4,5,0};
        solution.duplicateZeros(arr);

        System.out.println(Arrays.toString(arr));
    }
}